package com.huicai.poi.controller;

import com.huicai.poi.entity.Product;
import com.huicai.poi.service.PoiService;
import com.huicai.poi.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.util.Assert;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: MR.LIU
 * @description:
 * @date: 2020/6/7
 * @time: 16:38
 */
@RestController
public class PoiController {

    @Autowired
    private PoiService poiService;

    @PostMapping(value = "/upload")
    public Map<String, Object> leadDateDeal(@RequestParam(value = "file", required = false) MultipartFile file, @RequestParam String year) {
        Assert.notNull(file, "上传失败，请选择文件");
        String realPath = "E:/File/";
        Map<String, Object> map = poiService.importExcelData(realPath, file, year);
        boolean flag = (boolean) map.get("flag");
        if (flag) {
            map.put("msg", "导入成功");
        }
        return map;
    }

    @GetMapping(value = "/list")
    public Map<String, Object> searchProducts(){
        List<Product> products = poiService.searchProducts();
        Map<String, Object> map = new HashMap<>();
        map.put("title","产品列表");
        map.put("data",products);
        return ResultUtil.resultSuccess(map);
    }
}
